
package com.wjwh.heaven.common.security.utils;


import com.wjwh.heaven.common.security.pojo.SecurityUser;
import lombok.experimental.UtilityClass;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;

/**
 * 安全工具类
 *
 * @author L.cm
 */
@UtilityClass
public class SecurityUtils {
	/**
	 * 获取Authentication
	 */
	public Authentication getAuthentication() {
		return SecurityContextHolder.getContext().getAuthentication();
	}

	/**
	 * 获取用户
	 *
	 * @param authentication
	 * @return SecurityUser
	 * <p>
	 */
	public SecurityUser getUser(Authentication authentication) {
		Object principal = authentication.getPrincipal();
		if (principal instanceof SecurityUser) {
			return (SecurityUser) principal;
		}
		return null;
	}

	/**
	 * 获取用户
	 */
	public SecurityUser getUser() {
		Authentication authentication = getAuthentication();
		return getUser(authentication);
	}

//	/**
//	 * 获取用户角色信息
//	 *
//	 * @return 角色集合
//	 */
//	public List<Integer> getRoles() {
//		Authentication authentication = getAuthentication();
//		Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities();
//
//		List<Integer> roleIds = new ArrayList<>();
//		authorities.stream()
//				.filter(granted -> StrUtil.startWith(granted.getAuthority(), SecurityConstants.ROLE))
//				.forEach(granted -> {
//					String id = StrUtil.removePrefix(granted.getAuthority(), SecurityConstants.ROLE);
//					roleIds.add(Integer.parseInt(id));
//				});
//		return roleIds;
//	}

}
